﻿# -*- coding: utf-8 -*-
#############################
# Módulo:   dictionary_manager.py
# Autor:    Pablo Andrés Paliza
# Fecha:    2014/09
# Versión:  1.0
#
# Descripción: Modulo para administrar los diccionarios
# Crea el diccionario desde un archivo. Lee todas las palabras y guarda un diccionario con la palabra y la frecuencia de la palabra
###############################
import re
import sys
import getopt
import json
import collections
import codecs


dic_name = ""
dic_input= ""


def words(text): return re.findall(u'[a-zñáéíóúü0123456789]+', text.lower()) 

#verifica que los parametros sean validos
def check_parameters():
    global dic_input
    global dic_name
    print "Dic Name: " + dic_name
    print "Dic Input: " + dic_input
    if dic_name == "" or dic_input == "":
        print_help()

def create_dictionary():
    global dic_input
    global dic_name
    check_parameters()
    auxDict = collections.defaultdict(lambda: 1)    
    listToAdd=words(codecs.open(dic_input,'r','utf-8').read())               
    for f in listToAdd:       
        auxDict[f] += 1
    with open(dic_name, 'w') as f: f.write(json.dumps(auxDict))
    


#Warning corregir metodo para que tome parametros de entrada
# Adds a list of words to the dictionary from a plane txt
def add_to_dictionary():
    json_data = open('dictionaries\\dictClean.json')
    Dict = json.load(json_data)
    auxDict = collections.defaultdict(lambda: 1)    
    listToAdd=words(codecs.open('dictionaries\\cities.txt','r','utf-8').read())               
    for f in listToAdd:       
        auxDict[f] += 10000
    for f in Dict:       
        auxDict[f] += Dict[f]    
    with open('dictFinal.json', 'w') as f: f.write(json.dumps(auxDict))

#Eliminar palabras de un diccionario
def remove_from_dictionary():
    
    json_data = open('dictionaries/dictClean.json')
    Dict = json.load(json_data)
    json_data2 = open('dictionaries/namesNew.json')
    Dict2 = json.load(json_data2)
    print "Completo: " + str(len(Dict))
    print "Names: " +str(len(Dict2))         
    for f in Dict.keys():       
        if f in Dict2:
            Dict.pop(f)
    print "after: " +str(len(Dict))    
    with open('dictionaries/dictCleanSinNombres.json', 'w') as f: f.write(json.dumps(Dict))

def print_help():
    print """
Dictionary Manager: Administrador de diccionarios.
    
    -h, --help    Muestra esta ayuda. 
    -c, --create  Nombre del diccionario a crear
    -i, --input   Nombre del archivo de donde se va a crear el diccionario
    """
    sys.exit(0)

def main(argv):
    global dic_input
    global dic_name
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hc:i:",["help=","create=","input="])
    except getopt.GetoptError:
        print_help()
    for opt, arg in opts:
        if opt == '-h':
            print_help()           
        elif opt in ("-c", "--create"):
            dic_name = arg       
        elif opt in ("-i", "--input"):
            dic_input = arg
    #create_dictionary()  
    remove_from_dictionary()     
    
if __name__ == "__main__":
    main(sys.argv[1:])




#Refactorizacion spell_corrector
"""
def train():    
    features = words(codecs.open('dictionaries\\big.txt','r','utf-8').read())
    prefeatures=words(codecs.open('dictionaries\\specific.txt','r','utf-8').read())   
    names=words(codecs.open('dictionaries\\names.txt','r','utf-8').read())   
    aspell=words(codecs.open('dictionaries\\aspell.txt','r','utf-8').read())   
    model = collections.defaultdict(lambda: 1)
    #The words in prefeatures has more weight because the dictionary is more specific
    for f in aspell:
        model[f] += 1 
    for f in prefeatures:
        model[f] += 20000
    for f in names:
        model[f] += 10000
    for f in features:
        model[f] += 1 
    return model
"""
#NWORDS = train(words(file('big.txt').read()))
#NWORDS = train(words(codecs.open('big.txt','r','utf-8').read()))
#NWORDS = train()
